﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace SPCracker
{
    /// <summary>
    /// Main form for the application.
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the getCredentialsButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void getCredentialsButton_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                getCredentialsButton.Enabled = false;

                credentialsListView.Items.Clear();
                credentialsListView.SuspendLayout();
                foreach (var service in SPFarm.Local.Services)
                {
                    if (service is SPWebService)
                    {
                        var webSvc = (SPWebService)service;
                        foreach (var appPool in webSvc.ApplicationPools)
                        {
                            var itm = new ListViewItem(appPool.Name);
                            itm.SubItems.AddRange(new[] { appPool.Username, appPool.Password });
                            credentialsListView.Items.Add(itm);
                        }
                    }
                    if (service is SPWindowsService)
                    {
                        var winSvc = (SPWindowsService)service;
                        var itm = new ListViewItem(winSvc.Name);
                        itm.SubItems.AddRange(new[] { winSvc.ProcessIdentity.Username, winSvc.ProcessIdentity.Password });
                        credentialsListView.Items.Add(itm);
                    }
                }
                credentialsListView.ResumeLayout();
            }
            finally
            {
                Cursor = Cursors.Default;
                getCredentialsButton.Enabled = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the exitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Handles the Click event of the exportToExcelToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void exportToExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                getCredentialsButton.Enabled = false;
                using (var csv = new StreamWriter("spcracker.csv", false))
                {
                    // header
                    for (var col = 0; col < credentialsListView.Columns.Count; col++)
                    {
                        var listViewCol = credentialsListView.Columns[col];
                        if (col > 0)
                        {
                            csv.Write(',');
                        }
                        csv.Write(listViewCol.Text);
                    }
                    csv.Write("\r\n");

                    foreach (ListViewItem item in credentialsListView.Items)
                    {
                        for (var col = 0; col < item.SubItems.Count; col++)
                        {
                            if (col > 0)
                            {
                                csv.Write(',');
                            }
                            var subItem = item.SubItems[col];
                            csv.Write(subItem.Text);
                        }
                        csv.Write("\r\n");
                    }
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                getCredentialsButton.Enabled = true;
            }

            Process.Start("spcracker.csv");
        }
    }
}
